#include "precompiled.h"
#include "conditionsdialog.h"
using namespace std;
using namespace weo::ui;
ConditionsDialog::ConditionsDialog(QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/) : QDialog(parent, flags)
{
    setupUi(this);
    std::vector<std::string> keyvals;
    for(int i = 0;i<weo::engine::item::KeyValuesLength;i++)
    {
        keyvals.push_back(weo::engine::item::KeyValues[i]);
    }
    std::sort(keyvals.begin(),keyvals.end());
    std::vector<std::string>::const_iterator it;
    for(it = keyvals.begin(); it != keyvals.end(); it++)
    {
        AttributeComboBox->addItem(it->c_str());
    }
}
void ConditionsDialog::setConditions(const std::vector<std::pair<unsigned int, float>>& value)
{
    m_Conditions = value;
    std::vector<std::pair<unsigned int, float>>::const_iterator it;
    for(it = m_Conditions.begin(); it != m_Conditions.end(); it++)
    {
        ConditionsList->addItem(tr(weo::engine::item::KeyValues[it->first].c_str()));
    }
}
void ConditionsDialog::on_AddButton_clicked()
{
    if(ValueLineEdit->text().size() > 0)
    {
        int key = weo::engine::item::Attribute::getAttributeValue(AttributeComboBox->currentText().toStdString());
        int specialkey = weo::engine::item::Attribute::getAttributeValue("Special");
        if(ConditionsList->currentItem() != NULL && m_Conditions[ConditionsList->currentRow()].first == key && key != specialkey)
        {
            m_Conditions[ConditionsList->currentRow()] = std::pair<int,float>(key,boost::lexical_cast<float> (ValueLineEdit->text().toStdString()));
        }
        else
        {
            ConditionsList->addItem(AttributeComboBox->currentText());            
            m_Conditions.push_back(std::pair<int,float>(key,boost::lexical_cast<float> (ValueLineEdit->text().toStdString())));
        }
    }
}
void  ConditionsDialog::on_AttributeComboBox_currentIndexChanged(int value)
{
    ValueLineEdit->setText("");
}
void ConditionsDialog::on_DeleteButton_clicked()
{
    if(ConditionsList->currentItem() != NULL)
    {
        int index = ConditionsList->currentRow();
        std::vector<std::pair<unsigned int, float>>::iterator it = m_Conditions.begin();
        m_Conditions.erase(it+index);
        delete ConditionsList->takeItem(ConditionsList->currentRow());
    }
}
void ConditionsDialog::on_EditButton_clicked()
{
    if(ConditionsList->currentItem() != NULL)
    {
        AttributeComboBox->setCurrentIndex(AttributeComboBox->findText(ConditionsList->currentItem()->text()));
        float value = m_Conditions[ConditionsList->currentRow()].second;
        ValueLineEdit->setText(QString::number(value));
    }
}